/*
 * Copyright (c) 2020 - present, Inspur Genersoft Co., Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.inspur.edp.webservicecomponent.metadata.sourceInfo;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.inspur.edp.caf.cef.schema.base.utils.JsonUtil;
import com.inspur.edp.component.schema.IGspCompResource;
import com.inspur.edp.component.schema.resource.IApiResource;
import com.inspur.edp.component.schema.resource.RpcResource;

/**
 * @author Kaixuan Shi
 * @version 0.1
 */
public class DeprecatedSourceInfoDeserializer {

    private static final String CAF_SOURCE_TYPE = "cafIApi";
    private static final String IAPI_SOURCE_TYPE = "sgIApi";

    public static IGspCompResource deserialize(JsonNode sourceInfoNode) {
        String sourceType = JsonUtil.getPropValue(sourceInfoNode, "type", String.class);
        if (sourceType == null) {
            throw new RuntimeException("反序列化出错：缺少type字段，无法匹配序列化器。JsonNode:" + sourceInfoNode.toString());
        }
        switch (sourceType) {
            case CAF_SOURCE_TYPE:
                return deserializeCafInfo(sourceInfoNode);
            case IAPI_SOURCE_TYPE:
                return deserializeSgInfo(sourceInfoNode);
            default:
                throw new RuntimeException("Unsupported SourceType:" + sourceType);
        }
    }

    public static JsonNode serialize(IGspCompResource sourceInfo) {
        switch (sourceInfo.getKind()) {
            case RpcResource.KIND:
                return serializeCafInfo((RpcResource) sourceInfo);
            case IApiResource.KIND:
                return serializeSgInfo((IApiResource) sourceInfo);
            default:
                throw new RuntimeException("Unsupported SourceType:" + sourceInfo.getKind());
        }
    }

    private static JsonNode serializeCafInfo(RpcResource sourceInfo) {
        JsonNode sourceNode = (new ObjectMapper()).createObjectNode();
        JsonUtil.setPropValue(sourceNode, "type", CAF_SOURCE_TYPE);
        JsonUtil.setPropValue(sourceNode, "serviceUnit", sourceInfo.getServiceUnit());
        JsonUtil.setPropValue(sourceNode, "apiVersion", sourceInfo.getApiVersion());
        JsonUtil.setPropValue(sourceNode, "implementAssembly",
                sourceInfo.getClassName());
        JsonUtil.setPropValue(sourceNode, "interfaceAssembly",
                sourceInfo.getClassName());
        JsonUtil.setPropValue(sourceNode, "isCommonApi", sourceInfo.isCommonApi());

        return sourceNode;
    }

    private static JsonNode serializeSgInfo(IApiResource sourceInfo) {
        JsonNode sourceNode = (new ObjectMapper()).createObjectNode();
        JsonUtil.setPropValue(sourceNode, "type", IAPI_SOURCE_TYPE);
        JsonUtil.setPropValue(sourceNode, "apiId", sourceInfo.getApiId());
        JsonUtil.setPropValue(sourceNode, "apiCode", sourceInfo.getApiCode());
        JsonUtil.setPropValue(sourceNode, "apiName", sourceInfo.getApiName());
        JsonUtil.setPropValue(sourceNode, "apiSu", sourceInfo.getApiServiceUnit());
        JsonUtil.setPropValue(sourceNode, "apiUri", sourceInfo.getApiUri());

        return sourceNode;
    }

    private static RpcResource deserializeCafInfo(JsonNode node) {
        RpcResource resource = new RpcResource();
        resource.setServiceUnit(
                JsonUtil.getPropValue(node, "serviceUnit", String.class));
        resource.setApiVersion(JsonUtil.getPropValue(node, "apiVersion", String.class));
        resource.setClassName(JsonUtil.getPropValue(node, "implementAssembly",
                String.class));
        resource.setCommonApi(JsonUtil.getPropValue(node, "isCommonApi", boolean.class));

        return resource;
    }

    private static IApiResource deserializeSgInfo(JsonNode node) {
        IApiResource resource = new IApiResource();
        resource.setApiId(JsonUtil.getPropValue(node, "apiId", String.class));
        resource.setApiCode(JsonUtil.getPropValue(node, "apiCode", String.class));
        resource.setApiName(JsonUtil.getPropValue(node, "apiName", String.class));
        resource.setApiServiceUnit(JsonUtil.getPropValue(node, "apiSu", String.class));
        resource.setApiUri(JsonUtil.getPropValue(node, "apiUri", String.class));

        return resource;
    }
}
